package com.sixmac.controller.backend;

import com.sixmac.common.DataTableFactory;
import com.sixmac.controller.common.CommonController;
import com.sixmac.core.Constant;
import com.sixmac.entity.AppUser;
import com.sixmac.service.AccountService;
import com.sixmac.service.AppUserService;
import com.sixmac.service.OperaService;
import com.sixmac.utils.JsonUtil;
import com.sixmac.utils.WebUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * Created by Administrator on 2016/3/14 0014.
 */
@Controller
@RequestMapping(value = "backend/appUser")
public class AppUserController extends CommonController {

    @Autowired
    private AppUserService appUserService;

    @Autowired
    private AccountService accountService;

    @Autowired
    private OperaService operaService;

    @RequestMapping("index")
    public String index() {
        return "backend/APP用户列表";
    }

    @RequestMapping("/list")
    public void list(HttpServletResponse response,
                     String mobile,
                     Integer status,
                     Integer draw,
                     Integer start,
                     Integer length) {
        if (null == start || start == 0) {
            start = 1;
        }
        int pageNum = getPageNum(start, length);
        Page<AppUser> page = appUserService.page(mobile, status, pageNum, length);

        for (AppUser appUser : page.getContent()) {
            appUser.setAccountList(accountService.findListById(appUser.getId(), Constant.ACCOUNT_STATUS_App));
            appUser.setCount(appUser.getAccountList().size());
        }

        Map<String, Object> result = DataTableFactory.fitting(draw, page);
        WebUtil.printJson(response, result);
    }

    @RequestMapping("show")
    public String show(ModelMap model, Integer id) {
        if (null != id) {
            AppUser appUser = appUserService.getById(id);
            appUser.setAccountList(accountService.findListById(appUser.getId(), Constant.ACCOUNT_STATUS_App));
            model.addAttribute("appUser", appUser);
        }

        return "backend/APP用户详情";
    }

    /**
     * 启用 or 禁用APP用户
     *
     * @param userId
     * @param status
     * @return
     */
    @RequestMapping("/changeStatus")
    @ResponseBody
    public Integer changeStatus(HttpServletRequest request, Integer userId, Integer status) {
        try {
            AppUser appUser = appUserService.getById(userId);
            appUser.setStatus(status);

            appUserService.update(appUser);

            operaService.addOperaInfo(request, (status == 0 ? "启用" : "禁用") + "APP用户 " + appUser.getMobile());

            return 1;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 删除会员信息
     *
     * @param userId
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Integer delete(HttpServletRequest request, Integer userId) {
        try {
            appUserService.deleteById(request, userId);

            return 1;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 批量禁用会员信息
     *
     * @param ids
     * @return
     */
    @RequestMapping("/batchUpdate")
    @ResponseBody
    public Integer batchUpdate(HttpServletRequest request, String ids, Integer status) {
        try {
            int[] arrayId = JsonUtil.json2Obj(ids, int[].class);
            appUserService.updateAll(request, arrayId, status);

            return 1;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }
}
